package com.yuelife.service.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuelife.service.dao.SCategoryMapper;
import com.yuelife.service.pojo.SCategory;
import com.yuelife.service.service.SCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:admin
 * @Description:SCategory业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class SCategoryServiceImpl implements SCategoryService {

    @Autowired
    private SCategoryMapper sCategoryMapper;


    /**
     * SCategory条件+分页查询
     * @param sCategory 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<SCategory> findPage(SCategory sCategory, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(sCategory);
        //执行搜索
        return new PageInfo<SCategory>(sCategoryMapper.selectByExample(example));
    }

    /**
     * SCategory分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<SCategory> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<SCategory>(sCategoryMapper.selectAll());
    }

    /**
     * SCategory条件查询
     * @param sCategory
     * @return
     */
    @Override
    public List<SCategory> findList(SCategory sCategory){
        //构建查询条件
        Example example = createExample(sCategory);
        //根据构建的条件查询数据
        return sCategoryMapper.selectByExample(example);
    }


    /**
     * SCategory构建查询对象
     * @param sCategory
     * @return
     */
    public Example createExample(SCategory sCategory){
        Example example=new Example(SCategory.class);
        Example.Criteria criteria = example.createCriteria();
        if(sCategory!=null){
            // 店铺分类id
            if(!StringUtils.isEmpty(sCategory.getCategoryId())){
                    criteria.andEqualTo("categoryId",sCategory.getCategoryId());
            }
            // 店铺分类名称
            if(!StringUtils.isEmpty(sCategory.getCategoryName())){
                    criteria.andEqualTo("categoryName",sCategory.getCategoryName());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        sCategoryMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改SCategory
     * @param sCategory
     */
    @Override
    public void update(SCategory sCategory){
        sCategoryMapper.updateByPrimaryKey(sCategory);
    }

    /**
     * 增加SCategory
     * @param sCategory
     */
    @Override
    public void add(SCategory sCategory){
        sCategoryMapper.insert(sCategory);
    }

    /**
     * 根据ID查询SCategory
     * @param id
     * @return
     */
    @Override
    public SCategory findById(Integer id){
        return  sCategoryMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询SCategory全部数据
     * @return
     */
    @Override
    public List<SCategory> findAll() {
        return sCategoryMapper.selectAll();
    }
}
